package com.zlc.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.zlc.common.common.constant.NotificationConstant;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.common.service.ContextHolder;
import com.zlc.common.core.sys.mapper.MayanSysNotificationMapper;
import com.zlc.common.core.sys.model.entity.MayanSysNotification;
import com.zlc.common.core.sys.model.vo.MayanSysNotificationVo;
import com.zlc.common.core.sys.service.MayanSysNotificationService;
import com.zlc.common.model.sys.NotificationRequest;
import com.zlc.common.model.user.UserInfo;
import com.zlc.common.util.FluxUtil;
import com.zlc.common.util.JsonHelper;
import com.zlc.common.util.UserHelper;
import com.zlc.platform.service.SysNotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.List;

@Slf4j
@Service
public class SysNotificationServiceImpl implements SysNotificationService {


    @Autowired
    private MayanSysNotificationService mayanSysNotificationService;
    @Autowired
    private MayanSysNotificationMapper mayanSysNotificationMapper;
    @Autowired
    private FluxUtil fluxUtil;

    @Override
    public Flux<String> findNoShowSse() {
        UserInfo nowUser = UserHelper.getNowUser();
        return fluxUtil.createSSE(()->{
            ContextHolder.setUser(nowUser);
            MayanSysNotificationVo noRead = mayanSysNotificationMapper.findNoShow(nowUser.getUserAccount());
            if(noRead == null){
                return "";
            }
            noRead.setShowFlag(Boolean.TRUE);
            mayanSysNotificationService.saveVo(noRead);
            return JsonHelper.toJson(noRead);
        },()->"推送消息失败");
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<MayanSysNotificationVo> findList() {
        LambdaQueryWrapper<MayanSysNotification> wrapper = new LambdaQueryWrapper<>();
        wrapper.or()
                .eq(MayanSysNotification::getToUserId, UserHelper.getNowAccount())
                .or()
                .eq(MayanSysNotification::getNotificationType, NotificationConstant.NOTIFICATION_TYPE_PUBLIC)
                .orderByDesc(MayanSysNotification::getSendTime)
                .last(" limit 20 ");
        return mayanSysNotificationService.queryVoList(wrapper);
    }

    @Override
    public Integer findNoReadCount() {
        LambdaQueryWrapper<MayanSysNotification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MayanSysNotification::getToUserId, UserHelper.getNowAccount())
                .eq(MayanSysNotification::getReadFlag, false);
        return mayanSysNotificationService.count(wrapper);
    }

    @Override
    public Flux<String> findNoReadCountSse() {
        UserInfo nowUser = UserHelper.getNowUser();
        return fluxUtil.createSSE(()->{
            ContextHolder.setUser(nowUser);
            LambdaQueryWrapper<MayanSysNotification> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MayanSysNotification::getToUserId, UserHelper.getNowAccount())
                    .eq(MayanSysNotification::getReadFlag, false);
            return String.valueOf(mayanSysNotificationService.count(wrapper));
        },()->"推送消息失败");
    }

    @Override
    public PageVo<MayanSysNotificationVo> findPage(NotificationRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        request.setUserAccount(UserHelper.getNowAccount());
        List<MayanSysNotificationVo> list = mayanSysNotificationMapper.queryList(request);
        return PageVo.page(list);
    }

    @Override
    public void read(List<String> ids) {
        if(ObjectUtils.isEmpty(ids)){
            return;
        }
        LambdaUpdateWrapper<MayanSysNotification> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(MayanSysNotification::getReadFlag,true)
                .eq(MayanSysNotification::getToUserId,UserHelper.getNowAccount())
                .in(MayanSysNotification::getId,ids);
        mayanSysNotificationService.update(wrapper);
    }

    @Override
    public void readAll() {
        LambdaUpdateWrapper<MayanSysNotification> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(MayanSysNotification::getReadFlag,true)
                .eq(MayanSysNotification::getToUserId,UserHelper.getNowAccount());
        mayanSysNotificationService.update(wrapper);
    }

    @Override
    public void delete(List<String> ids) {
        mayanSysNotificationMapper.remove(ids,UserHelper.getNowAccount());
    }
}
